package com.db.httpserver;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.atomic.AtomicInteger;

public final class WorkerThread implements Runnable {

	private Socket connection;
	
	private static AtomicInteger succesfulConnectionsCounter = new AtomicInteger(0);
	public static AtomicInteger getSuccesfulConnectionsCounter() {
		return succesfulConnectionsCounter;
	}

	public WorkerThread(Socket socket) throws SocketException {
		connection = socket;
		connection.setSoTimeout(Parameter.getTimeout() );
	}
	
    public void run() {
    	try {
			(new Worker()).handleRequest(connection);
			   succesfulConnectionsCounter.getAndIncrement();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    public static void startPool(int suggestedPort) throws IOException {
    	Parameter.setPort(suggestedPort);
    	Parameter.print();
    	int port = Parameter.getPort();
    	
    	Server server = new Server(port);
    	server.run();
    }
}